﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Tools;

namespace Pixysoft.Weblications.Sango.Command.pvp_resource
{
    partial class SangoPvpResourceHandler
    {
        public pvpresource_harvest pvpresource_harvest(long positionindex)
        {
            string userCode = SangoManager.Authentication();

            pvpresource_harvest response = ReflectionManager.CreatePojo<pvpresource_harvest>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);
            Entities.Usr_Section section = UsrSectionDao.GetSection(userCode);
            Entities.Usr_Activity activity = UsrActivityDao.getHistory(userCode);


            if (activity.Resourceharvestcount >= ResourceFormular.default_max_harvestcount)
            {
                response.Message = "资源收获到达次数已满，请等候1小时";
                return response;
            }

            Entities.Usr_Resource resource = UsrResourceDao.TryGetOccupyResource(
                section.Sectionserialcode, positionindex);

            if (resource == null)
            {
                response.Message = "资源不存在";
                return response;
            }

            Entities.Usr_Profile targetprofile = UsrProfileDao.getUsrProfile(resource.Usercode);

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                if (ResourceFormular.resourceNeedBalance(resource))
                {
                    ResourceFormular.resourceCurrentBalance(resource, targetprofile);
                    command.Update(resource);
                    resource = null;
                }

                if (resource == null)
                {
                    command.Update(targetprofile);
                    command.Commit();

                    response.Message = "资源不存在";
                    return;
                }

                activity.Resourceharvestcount = activity.Resourceharvestcount + 1;
                activity.Resourceharvestdate = GlobalTimer.Instance.ToTimeSequence();

                ResourceFormular.resourceHarvest(resource, profile, targetprofile);
                command.Update(resource);
                command.Update(profile);
                command.Update(targetprofile);
                command.Update(activity);
                command.Commit();

                response.profile = UsrProfileFacade.ToProfile(profile);
                response.Succeed = true;
            });

            return response;
        }
    }

    public interface pvpresource_harvest : ISangoResponse
    {
        player_profile profile { get;set;}
    }
}
